import {Component, Input, OnInit} from '@angular/core';
import {SysFunction, SysFunctionCondition} from '../../sys-function.type';
import {ActivatedRoute, Router} from '@angular/router';
import {Services} from '../../../../../frame/module/services';
import {ErrRes} from '../../../../../frame/global/err-res';
import {isNullOrUndefined} from 'util';
import {UrlRou} from '../../../../../frame';
import {ModifySysFunctionComponent} from '../modify-sys-function.component';

@Component({
    selector: 'app-main-modify-sys-function',
    templateUrl: './main-modify-sys-function.component.html',
    styleUrls: ['./main-modify-sys-function.component.css']
})
export class MainModifySysFunctionComponent implements OnInit {

    errRes = new ErrRes();

    domain: SysFunction = new SysFunction();

    extend = {
        domain: {},
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: SysFunctionCondition = new SysFunctionCondition();
            condition.uuid = uuid;
            Services.sysFunction.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.domain = item.domain;
                }
            });
        },
        update: {
            req: (event) => {
                Services.sysFunction.update(this.domain, this.func.update.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    this.router.navigate([UrlRou.sysFunction.list]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(public modifySysFunction: ModifySysFunctionComponent, private router: Router, private route: ActivatedRoute) {
    }

    ngOnInit() {
        this.modifySysFunction.main = this;
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.func.loadDomain(uuid);
    }

}
